package com.niit.agricstore.controller;

import com.github.pagehelper.PageInfo;
import com.niit.agricstore.entity.Product;
import com.niit.agricstore.entity.Type;
import com.niit.agricstore.entity.User;
import com.niit.agricstore.service.TypeService;
import com.niit.agricstore.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/type")
public class TypeController {

    private TypeService typeService;
    @Autowired
    public void setTyperService(TypeService typeService) {

        this.typeService = typeService;
    }

    @GetMapping("/selectTypes/all")
    public Result selectTypesByPageAll(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        PageInfo pageInfo = typeService.selectAllTypesByPage(pageNum, pageSize);
        if (pageInfo != null) {
            return Result.success("200", "分页显示所有商品成功", pageInfo);
        } else {
            return Result.error("202", "分页显示所有商品失败");
        }
    }
    @GetMapping("/selectById/{id}")
    public Result<Type> getCurrentUserInfo(@PathVariable("id")Long id) {
        Type type = typeService.getCurrentType(id);
        return Result.success(type);}

    @DeleteMapping("/deltypeId/{typeId}")
    public Result<Void> deleteType(@PathVariable int typeId) {
        int rowsAffected = typeService.deleteType(typeId);
        if (rowsAffected > 0) {
            return Result.success();
        } else {
            return Result.error("202","Notice not found");
        }
    }
    @PostMapping("add_type")
    public Result addType(@RequestBody Map<String, Object> requestData) {
        String typeName = (String) requestData.get("typeName");
        String typeDescription = (String) requestData.get("typeDescription");
        Type type=new Type();
        type.setTypeName(typeName);
        type.setTypeDescription(typeDescription);
        int row = typeService.addTypes(type);
        if (row > 0) {
            return Result.success("200", "新增单品成功", type);
        } else {
            return Result.error("202", "新增单品失败~~~");
        }
    }
    @GetMapping("/selectType/{typeId}")
    public Result selectTypeById(@PathVariable("typeId") Integer typeId){
        Type type= typeService.selectTypeById(typeId);
        if(type!=null){
            return Result.success("200","根据产品ID查询成功",type);
        }else {
            return Result.error("202", "根据产品ID查询失败");
        }
    }

    @PutMapping("/update")
    public Result updateById(@RequestBody Type type ) {
        typeService.updateById(type);
        return Result.success();
    }



}
